package cmj.cloudmusic.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import cmj.cloudmusic.R;


public class MyPlayerActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton back;
    private ImageButton play;
    private ImageButton to;
    private EditText url;
    private MediaPlayer player;
    private SeekBar seekBar;
    private boolean seekbarstate;

    private Timer timer;
    private TimerTask timerTask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_player);
        back= (ImageButton) findViewById(R.id.back);
        play= (ImageButton) findViewById(R.id.play);
        to= (ImageButton) findViewById(R.id.to);
        url= (EditText) findViewById(R.id.url);
        seekBar= (SeekBar) findViewById(R.id.seekbar);

        back.setOnClickListener(this);
        play.setOnClickListener(this);
        to.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seekbarstate=true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (player==null){
                    startPlay();
                }
                player.seekTo(seekBar.getProgress());
                seekbarstate=false;
            }
        });
    }


    private void initTimer(){
        seekBar.setMax(player.getDuration());
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                //设置进度条s
                if (seekbarstate)return;
                seekBar.setProgress(player.getCurrentPosition());
            }
        };
        timer.schedule(timerTask,0,1000);
    }

    private void back(){
        int currentPosition =player.getCurrentPosition();
        if (currentPosition<5000)return;
        player.seekTo(currentPosition-5000);
    }
    private void to() {
        int currentPosition = player.getCurrentPosition();
        int totalLength = player.getDuration();
        if (totalLength < currentPosition + 5000) return;
        player.seekTo(currentPosition + 5000);
    }

    private void stopPlay() {
        if(player!=null) {
            //停止播放
            player.stop();
            //释放资源
            player.release();
            //对象置空
            player=null;
        }
    }

    private void restartPlay() {
        if(player!=null) {
            //重置媒体
            player.reset();
            player=null;
        }
        startPlay();
    }

    /**
     * 播放
     */
    private void startPlay() {
        if(player!=null) {
            player.start();
        } else {
            player=new MediaPlayer();
            Uri uri=Uri.parse(url.getText().toString());
            try {
                player.setDataSource(this, uri);
                //异步加载媒体流
                player.prepareAsync();
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        //设置进度
                        initTimer();
                        //准备完毕
                        player.start();
                    }
                });
                player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        //错误监听
                        Toast.makeText(MyPlayerActivity.this, "无网络或歌曲链接无效", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        //播放完毕
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        stopPlay();
        super.onDestroy();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                back();
                break;
            case R.id.play:
                if (play!=null&&player.isPlaying()){
                    stopPlay();
                }else {
                    startPlay();
                }
                break;
            case R.id.to:
                to();
                break;

        }
    }
}
